昨天提到一維結構的資料管理,並有簡單儲存數值的基礎概念,透過combine function c()便可儲存多個資料。
"每天媽媽都會給恩君零用錢,上禮拜去了獸人賭場賭了一把,平時也會花花小錢買點零嘴,但到底花了多少恩君真是一點概念都沒有耶,這樣何時才能買套裝備去冒險呢?"
"來吧,首先試試把上禮拜的消費帶入吧!"
零用金用petty_cash代替
petty_cash <- c(20, 50, 30, 65, 38, 79)
petty_cash
花費則用負號expense_vector代替
expense_vector <- c(-15, -10, -49, 0, -32, -80)
expense_vector
賭場則用poker_vector
poker_vector <- c(-20, 15, -18, 12, -50, 15)
poker_vector
"好啦,數據儲存了以後還是沒有概念阿,恩君還是不知道花了多少錢"
"別急別急,我們可以透過相加來知道當日開銷,在用sum()來計算總額。對了還有names()來命名星期或日子"
total_cost <- expense_vector + poker_vector
total_cost
[1] -35 5 -67 12 -82 -65
sum(total_cost)
[1] -232
sum(petty_cash)
[1] 282
whats_left <- petty_cash + total_cost
whats_left
[1] -15 55 -37 77 -44 14
sum(whats_left)
[1] 50
最後利用names()來命名抬頭,也可替代為日期
days_vector <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(whats_left) <- days_vector
whats_left
Sunday Monday Tuesday Wednesday Thursday Friday
-15 55 -37 77 -44 14
"好了,這樣總和,及對應星期都非常清楚了"